﻿using DotNetMVC.OA.IBLL;
using DotNetMVC.OA.Model;
using DotNetMVC.OA.Model.Param;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace DotNetMVC.OA.UI.Portal.Controllers
{
    public class RoleInfoController : BaseController
    {
        public IRoleInfoService RoleInfoService { get; set; }
        // GET: RoleInfo
        public ActionResult Index()
        {
            return View();
        }

        #region 查询
        /// <summary>
        /// 获取用户信息
        /// </summary>
        /// <returns></returns>
        public ActionResult GetAllRoleInfos()
        {
            //jquery easyui table:{total:50,rows:[{},{}]}
            //easyui table 在初始化的时候自动发送一下两个参数值
            int pageSize = int.Parse(Request["rows"] ?? "10");
            int pageIndex = int.Parse(Request["page"] ?? "1");

            //过滤用户名、备注   
            string schName = Request["schName"];
            string schRemark = Request["schRemark"];
            //初始化查询条件
            var queryParam = new RoleQueryParam
            {
                PageIndex = pageIndex,
                PageSize = pageSize,
                Total = 0,
                SchName = schName,
                SchRemark = schRemark
            };

            var pageData = RoleInfoService.LoagPageData(queryParam);

            int total = queryParam.Total;

            var temp = pageData.Select(r => new { r.ID,r.Remark,r.SubTime, r.ModfiedOn, r.DelFlag,r.RoleName });
            var data = new { total = total, rows = temp.ToList() };
            return Json(data, JsonRequestBehavior.AllowGet);
        }
        #endregion

        #region 添加
        [HttpPost]
        public ActionResult Add(RoleInfo roleInfo)
        {
            roleInfo.DelFlag = (short)Model.Enum.DelFlagEnum.Normal;
            roleInfo.ModfiedOn = DateTime.Now;
            roleInfo.SubTime = DateTime.Now;

            RoleInfoService.Add(roleInfo);
            return Content("ok");
        }
        #endregion

        #region 删除
        public ActionResult BatchDelete(string ids)
        {
            if (string.IsNullOrEmpty(ids))
            {
                return Content("请选中要删除的数据");
            }
            string[] strIds = ids.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
            List<long> idList = new List<long>();
            foreach (var strId in strIds)
            {
                idList.Add(long.Parse(strId));
            }
            RoleInfoService.Delete(idList);
            return Content("ok");
        }

        public ActionResult Delete(long id)
        {
            RoleInfoService.Delete(id);
            return Content("ok");
        }
        #endregion

        #region 修改
        [HttpGet]
        public ActionResult Edit(long id)
        {
            ViewData.Model = RoleInfoService.GetEntities(u => u.ID == id).FirstOrDefault();
            return View();
        }

        [HttpPost]
        public ActionResult Edit(RoleInfo roleInfo)
        {
            RoleInfoService.Update(roleInfo);
            return Content("ok");
        }
        #endregion
    }
}